package com.evan.designpatterns.proxy.dynamicproxy.jdkproxy;

import com.evan.designpatterns.proxy.Person;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author :liujiapeng01
 * @date :2019-03-14 19:19
 **/
public class Agency implements InvocationHandler {

    private Person person;

    public Person getInstance(Person person) {
        this.person = person;
        Class<? extends Person> aClass = person.getClass();
        return (Person) Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(), this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(person, args);
        after();
        return invoke;
    }

    private void after() {
        System.out.println("收钱");
    }

    private void before() {
        System.out.println("宣传");

    }
}
